var express = require("express");
const {
  OrderModel,
  AfterSaleModel,
  DeliverGoodsModel,
  ListModel,
  cateModel,
  InventoryModel,
} = require("../../model/model");
const { ObjectId } = require("mongodb");
var router = express.Router();

router.get("/order", async (req, res) => {
  let { page, name, orderId, consignee, status, phone, st, et } = req.query;
  let queryValue = [{}];
  let ifname = new RegExp(name);
  console.log(name == "undefined");
  //   if(orderId) queryValue.push({_id:ifid});
  if (name) {
    queryValue.push({ name: ifname });
  }
  if (consignee) queryValue.push({ consignee: new RegExp(consignee) });
  if (status) queryValue.push({ status: status });
  if (phone) queryValue.push({ phone: new RegExp(phone) });
  if (st) queryValue.push({ cTime: { $gt: new Date(st) } });
  if (et) queryValue.push({ cTime: { $lt: new Date(et) } });
  console.log(queryValue);
  let list = await OrderModel.find({ $and: queryValue })
    .skip((page - 1) * 10)
    .limit(10);
  console.log(list);
  let total = (await OrderModel.find({ $and: queryValue })).length;
  res.send({
    code: 200,
    data: list,
    total,
  });
});

router.get("/aftersale", async (req, res) => {
  let { page, name, orderId, tname, tcate, cate } = req.query;
  let queryValue = [{}];
  if (name) queryValue.push({ name: new RegExp(name) });
  if (orderId) queryValue.push({ _id: orderId });
  if (tname) queryValue.push({ tname: new RegExp(tname) });
  if (tcate) queryValue.push({ tcate: tcate });
  if (cate) queryValue.push({ cate: cate });
  let list = await AfterSaleModel.find({ $and: queryValue })
    .skip((page - 1) * 10)
    .limit(10);
  let total = (await AfterSaleModel.find({ $and: queryValue })).length;
  res.send({
    code: 200,
    list,
    total,
  });
});

router.get("/delivergoods", async (req, res) => {
  let { page = 1 } = req.query;
  let list = await DeliverGoodsModel.find().populate("orderId");
  console.log(list);
  res.send({
    code: 200,
    list,
  });
});

router.get("/shoplist", async (req, res) => {
  let { page = 1 } = req.query;
  let list = await ListModel.find()
    .skip((page - 1) * 10)
    .limit(10);
  let total = (await ListModel.find()).length;
  res.send({
    code: 200,
    list,
    total,
  });
});

router.post("/addshop", (req, res) => {
  let obj = req.body;
  console.log(obj);
  
  ListModel.create(obj);
  res.send({
    code: 200,
    list,
  });
});

router.get("/catelist", async (req, res) => {
  let { page = 1 } = req.query;
  let list = await cateModel
    .find()
    .skip((page - 1) * 10)
    .limit(10);
  let total = (await cateModel.find()).length;
  res.send({
    code: 200,
    list,
    total,
  });
});

router.get("/inventorylist", async (req, res) => {
  let { page = 1 } = req.query;
  let list = await InventoryModel.find()
    .skip((page - 1) * 10)
    .limit(10);
  let total = (await InventoryModel.find()).length;
  InventoryModel.create({
    name: "测试",
    type: 1,
    delivery: 100,
    residue: 100,
    aname: "测试人",
  });
  res.send({
    code: 200,
    list,
    total,
  });
});

module.exports = router;
